<?
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();
  
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delZapataGroup($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["zapatagrp"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["zapatagrp"]["alertDelBad"]."</div>";
  }
  
  $ret=$asterisk->getZapataGroup(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id       = $v["id"];
      $name     = $v["name"];
      $type     = $v["type"];
      $group    = $v["group"];
 //   $ports    = implode(",", $v["port"]); // id from cw_zapata_channel
      $portList = $v["port"][0];
      $fromItem = $portList;
      $lastItem = $portList;

      foreach ($v["port"] as $p) {
        if($fromItem == $p) continue;

        if ( ($p - 1) == $lastItem) {
          $lastItem=$p;
          continue;
        } 
        if ($fromItem != $lastItem) {
          $portList.= "-";
          $portList.= $lastItem;
        }
        $portList.= ",";
        $portList.= "$p";
        $fromItem = $p;
        $lastItem = $p;
      }
      if ($fromItem != $lastItem) {
        $portList.= "-";
        $portList.= $p;
      }

      //$html.="<td><a href=\"edit/?id=$id\">$name</a> ($portType ports: <strong>$ports</strong>) <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Are you sure to delete the $name group?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      $html.="<td><a href=\"edit/?id=$id\">$name</a>&nbsp;&nbsp;($type)&nbsp;&nbsp;Channel-Id:&nbsp;(<strong>$portList</strong>)&nbsp;<a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Are you sure to delete the $name group?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      if ((++$count)%1==0) $html.="</tr><tr>\n";
    //if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <? $sel="groups"; include ("navigation.php");?>

  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["zapatagrp"]["title"]?></h1>
    <h3>
      <a href="edit/?portType=PRI"   class="admin" ><?=$cwlang["zapatagrp"][""]?>New PRI port Group</a> &nbsp;&nbsp;
      <a href="edit/?portType=E%26M" class="admin" ><?=$cwlang["zapatagrp"][""]?>New E&amp;M port Group</a> &nbsp;&nbsp;
      <a href="edit/?portType=FXO"   class="admin" ><?=$cwlang["zapatagrp"][""]?>New FXO port Group</a> &nbsp;&nbsp;
      <a href="edit/?portType=FXS"   class="admin" ><?=$cwlang["zapatagrp"][""]?>New FXS port Group</a> 
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?$sel="zapata";include("../sidebar.php");?>
          <h1><?=$cwlang["zapatagrp"]["sidebarTitle"]?></h1>
          <?=$cwlang["zapatagrp"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "zapata");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

